﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

using BigLotterySyndicate.Classes;
using SyndicateBusiness.Classes;


namespace BigLotterySyndicate.Controls
{
    public class PageIndexChangedEvetArgs : EventArgs
    {
        public int PageIndex { get; set; }

        public PageIndexChangedEvetArgs(int index)
        {
            this.PageIndex = index;
        }
    }

    public partial class Pager : System.Web.UI.UserControl
    {
        public string PageIndexPageParam { get; set; }

        public string PageSizePageParam { get; set; }

        public event EventHandler<PageIndexChangedEvetArgs> PageIndexChanged;

        private string _FirstText = "First";
        public string FirstText
        {
            get
            {
                return this._FirstText;
            }
            set
            {
                this._FirstText = value;
            }
        }

        private string _PreviousText = "Previous";
        public string PreviousText
        {
            get
            {
                return this._PreviousText;
            }
            set
            {
                this._PreviousText = value;
            }
        }

        private string _NextText = "Next";
        public string NextText
        {
            get
            {
                return this._NextText;
            }
            set
            {
                this._NextText = value;
            }
        }

        private string _LastText = "Last";
        public string LastText
        {
            get
            {
                return this._LastText;
            }
            set
            {
                this._LastText = value;
            }
        }

        public int PageIndex
        {
            get
            {
                if (this.ViewState["PageIndex"] == null)
                {
                    if (this.Request.QueryString[this.PageIndexPageParam] != null)
                        this.PageIndex = Utilities.StringToInt(this.Request.QueryString[this.PageIndexPageParam], 1).Value;
                    else
                        this.PageIndex = 1;
                }

                return (int)this.ViewState["PageIndex"];
            }
            set
            {
                this.ViewState["PageIndex"] = value;
                this.BindPages();
            }
        }

        public int PageSize
        {
            get
            {
                if (this.ViewState["PageSize"] == null)
                {
                    if (this.Request.QueryString[this.PageSizePageParam] != null)
                        this.PageSize = Utilities.StringToInt(this.Request.QueryString[this.PageSizePageParam], 0).Value;
                    else
                        this.PageSize = 0;
                }

                return (int)this.ViewState["PageSize"];
            }
            set
            {
                this.ViewState["PageSize"] = value;
            }
        }

        public int RecordCount
        {
            get
            {
                if (this.ViewState["RecordCount"] == null)
                    this.RecordCount = 0;

                return (int)this.ViewState["RecordCount"];
            }
            set
            {
                this.ViewState["RecordCount"] = value;

                if (this.PageSize > 0)
                    this.ViewState["PageCount"] = (value / this.PageSize) + (value % this.PageSize > 0 ? 1 : 0);
            }
        }

        public int PageCount
        {
            get
            {
                if (this.ViewState["PageCount"] == null)
                    this.PageCount = 0;

                return (int)this.ViewState["PageCount"];
            }
            set
            {
                this.ViewState["PageCount"] = value;

                this.ViewState["RecordCount"] = value * this.PageSize;
            }
        }

        public int PageWindow
        {
            get
            {
                if (this.ViewState["PageWindow"] == null)
                    this.PageWindow = 4;

                return (int)this.ViewState["PageWindow"];
            }
            set
            {
                this.ViewState["PageWindow"] = value;
            }
        }

        public bool ShowPageWindow
        {
            get
            {
                if (this.ViewState["ShowPageWindow"] == null)
                    return true;

                return (bool)this.ViewState["ShowPageWindow"];
            }
            set
            {
                this.ViewState["ShowPageWindow"] = value;
            }
        }

        private int[] PagesInWindow
        {
            get
            {
                int startIndex = Math.Max(Math.Min(this.PageIndex - this.PageWindow / 2, this.PageCount - this.PageWindow), 1);
                int endIndex = Math.Min(startIndex + this.PageWindow, this.PageCount);

                int[] pages = new int[endIndex - startIndex + 1];

                for (int i = startIndex; i <= endIndex; ++i)
                    pages[i - startIndex] = i;

                return pages;
            }
        }

        public int StartIndex
        {
            get
            {
                return (this.PageIndex - 1) * this.PageSize + 1;
            }
        }

        protected void OnPageIndexChanged(PageIndexChangedEvetArgs args)
        {
            if (this.PageIndexChanged != null)
                this.PageIndexChanged(this, args);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Visible)
                return;


            this.SetPages();
        }

        public void Refresh()
        {
            this.BindPages();
        }

        protected void Page_PreRender(object sender, EventArgs e)
        {
            this.Visible = (this.PageCount > 1);
            this.PagesPlaceHolder.Visible = (this.ShowPageWindow && this.PageCount > 1);
            // first
            this.FirstPlaceHolder.Visible = (this.PageIndex > 1);
            this.FirstPlaceHolderDisabled.Visible = (this.PageIndex == 1);
            // prev
            this.PreviousPlaceHolder.Visible = (this.PageIndex > 1);
            this.PreviousPlaceHolderDisabled.Visible = (this.PageIndex == 1);
            // next
            this.NextPlaceHolder.Visible = (this.PageIndex < this.PageCount);
            this.NextPlaceHolderDisabled.Visible = (this.PageIndex >= this.PageCount);
            // last
            this.LastPlaceHolder.Visible = (this.PageIndex < this.PageCount);
            this.LastPlaceHolderDisabled.Visible = (this.PageIndex >= this.PageCount);
        }

        protected void SetPages()
        {
            if (this.IsPostBack)
                return;

            this.BindPages();
        }

        protected void BindPages()
        {
            this.PagesRepeater.DataSource = this.PagesInWindow;
            this.PagesRepeater.DataBind();
        }

        protected void Pager_Command(object sender, CommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "FirstSelect":
                    this.SetPageIndex(1);
                    break;
                case "PreviousSelect":
                    this.SetPageIndex(Math.Max(this.PageIndex - 1, 1));
                    break;
                case "NextSelect":
                    this.SetPageIndex(Math.Min(this.PageIndex + 1, this.PageCount));
                    break;
                case "LastSelect":
                    this.SetPageIndex(this.PageCount);
                    break;
                default:
                    this.SetPageIndex(1);
                    break;
            }
        }

        protected void PagesRepeater_ItemCommand(object sender, CommandEventArgs e)
        {
            int pageIndex = Utilities.StringToInt((string)e.CommandArgument, 1).Value;

            switch (e.CommandName)
            {
                case "PageSelect":
                    this.SetPageIndex(pageIndex);
                    break;
                default:
                    this.SetPageIndex(1);
                    break;
            }
        }

        protected void SetPageIndex(int pageIndex)
        {
            int prev = this.PageIndex;

            this.PageIndex = pageIndex;

            if (prev != this.PageIndex)
            {
                this.BindPages();
                this.OnPageIndexChanged(new PageIndexChangedEvetArgs(this.PageIndex));
            }
        }

        protected string PrintPageIndex(object data)
        {
            int pageIndex = (int)data;

            return pageIndex.ToString();
        }

        protected string PrintFirstText()
        {
            return this.FirstText;
        }

        protected string PrintPreviousText()
        {
            return this.PreviousText;
        }

        protected string PrintNextText()
        {
            return this.NextText;
        }

        protected string PrintLastText()
        {
            return this.LastText;
        }

        protected string PrintShowPageWindow()
        {
            return this.ShowPageWindow.ToString();
        }


    }
}